#include "stdafx.h"
#include "models.h"
#include "cResourceManager.h"
Models::Models()
{
	vert = NULL;
	indices = NULL;
}
Models::~Models()
{
	if(vert)
		delete vert;
	if(indices)
		delete indices;
	vert = NULL;
	indices = NULL;
	glDeleteBuffers(2, vboIds);
}
int Models::Init(const char *fileName)
{
	FILE *f;
	char line[256];
	int verCount=0;
	int indexCount = 0;
	int dusty;
	f = fopen (fileName, "rt");  
	if(f == NULL)
		return 0;
	fgets(line, 255, f);
	sscanf_s(line,"NrVertices: %d",&verCount);
	printf("NrVertices: %d\n",verCount);
	numVertex = verCount;
	vert = new Vertex[verCount];

    for(int i = 0;i < verCount; ++i)
	{
		fgets(line, 255, f);
		sscanf_s(line, "%d. pos:[%f, %f, %f]; norm:[%f, %f, %f]; binorm:[%f, %f, %f]; tgt:[%f, %f, %f]; uv:[%f, %f];",
			&dusty, &vert[i].pos.x, &vert[i].pos.y, &vert[i].pos.z,
			&vert[i].norm.x, &vert[i].norm.y, &vert[i].norm.z,
			&vert[i].biNorm.x, &vert[i].biNorm.y, &vert[i].biNorm.z,
			&vert[i].tgt.x, &vert[i].tgt.y, &vert[i].tgt.z,
			&vert[i].uv.x, &vert[i].uv.y);
	}
	fgets(line, 255, f);
	sscanf_s(line, "NrIndices: %d",&indexCount);
	numIndices = indexCount;
	printf("NrIndices: %d\n",indexCount);
	indices=new int[indexCount];
	for(int i = 0; i < indexCount; i = i+3)
	{
		fgets(line, 255, f);
		sscanf_s(line,"%d. %d, %d, %d",&dusty,&indices[i],&indices[i+1],&indices[i+2]);
	}
	fclose(f);	
	glGenBuffers(2, vboIds);
	glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*numVertex,vert, GL_STATIC_DRAW);	
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[1]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(int) * numIndices, indices, GL_STATIC_DRAW);
	
	ClearUp();
	return 1;
}
int Models::Init(const char *fileName,int idtexture)
{
	FILE *f;
	char line[256];
	int verCount=0;
	int indexCount = 0;
	int dusty;
	f = fopen (fileName, "rt");  
	if(f == NULL)
		return 0;
	fgets(line, 255, f);
	sscanf_s(line,"NrVertices: %d",&verCount);
	printf("NrVertices: %d\n",verCount);
	numVertex = verCount;
	vert = new Vertex[verCount];
	int sizeOfType;
	cTexture *mtexture = cResourceManager::GetInstance()->GetTexture(idtexture);
	if(mtexture->bpp = 24) sizeOfType = 3;

    for(int i = 0;i < verCount; ++i)
	{
		fgets(line, 255, f);
		sscanf_s(line, "%d. pos:[%f, %f, %f]; norm:[%f, %f, %f]; binorm:[%f, %f, %f]; tgt:[%f, %f, %f]; uv:[%f, %f];",
			&dusty, &vert[i].pos.x, &vert[i].pos.y, &vert[i].pos.z,
			&vert[i].norm.x, &vert[i].norm.y, &vert[i].norm.z,
			&vert[i].biNorm.x, &vert[i].biNorm.y, &vert[i].biNorm.z,
			&vert[i].tgt.x, &vert[i].tgt.y, &vert[i].tgt.z,
			&vert[i].uv.x, &vert[i].uv.y);
	//recalculate height
		
		
	//	int r = (int)(unsigned char)mtexture->colorBuffer[((int)(vert[i].uv.y * (mtexture->height-1))*mtexture->width*sizeOfType)+((int)(vert[i].uv.x *(mtexture->width-1))*sizeOfType)];
	//	int g = (int)(unsigned char)mtexture->colorBuffer[((int)(vert[i].uv.y * (mtexture->height-1))*mtexture->width*sizeOfType)+((int)(vert[i].uv.x *(mtexture->width-1))*sizeOfType)+1];
	//	int b = (int)(unsigned char)mtexture->colorBuffer[((int)(vert[i].uv.y * (mtexture->height-1))*mtexture->width*sizeOfType)+((int)(vert[i].uv.x *(mtexture->width-1))*sizeOfType)+2];
		
	//	vert[i].pos.y = (float)(r * 3 + g * 2 + b) / 255.0 * 10;


	}
	fgets(line, 255, f);
	sscanf_s(line, "NrIndices: %d",&indexCount);
	numIndices = indexCount;
	printf("NrIndices: %d\n",indexCount);
	indices=new int[indexCount];
	for(int i = 0; i < indexCount; i = i+3)
	{
		fgets(line, 255, f);
		sscanf_s(line,"%d. %d, %d, %d",&dusty,&indices[i],&indices[i+1],&indices[i+2]);
	}
	fclose(f);	
	glGenBuffers(2, vboIds);
	glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*numVertex,vert, GL_STATIC_DRAW);	
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[1]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(int) * numIndices, indices, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER,0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
	ClearUp();
	return 1;
}
void Models::ClearUp()
{
	if(vert)
		delete vert;
	if(indices)
		delete indices;
	vert = NULL;
	indices = NULL;
}
